import { execSync } from 'child_process'
import { WinCharEncodeCodeFormatMap } from '../maps'

/**
 * 获取当前操作系统的编码格式
 *
 * @returns {string} 当前操作系统的编码格式
 */
export const getSysEncodeFormat = (): string => {
  if (process.platform === 'win32') {
    const encodeCode = execSync('chcp', { encoding: 'utf8' }).match(/\d+/)?.[0] || ''
    return (WinCharEncodeCodeFormatMap as Record<string, string>)[encodeCode] || 'utf8'
  } else {
    const locale = process.env.LANG || 'en_US.UTF-8'
    return locale.split('.')[1]?.toLowerCase() || 'utf8'
  }
}

export type GetSysEncodeFormatType = typeof getSysEncodeFormat
